import requests
from urllib import parse
from qcloudsms_py import SmsSingleSender
from finance_c2c.local_settings import MoFangPWD, MoFangUid, MoFangRequest
from core.tools.encryption import hashlib_md5


class ShortMessage(object):
    @classmethod
    def send_single(cls, tel, code, template_id, appid, appkey):
        """
        :param tel:
        :param code: 验证码
        :param template_id: 模板id
        :param appid: 短信应用id
        :param appkey: 短信key
        1400098620 零号用的
        :return:
        """
        ssender = SmsSingleSender(appid, appkey)
        # 89021 	您的验证码是{1}，您正在使用卡来宝公众号，请勿将验证码告诉他人。
        # 108980 	您的验证码是{1}，您正在使用必备的公众号，请勿将验证码告诉他人。
        # 133521 短信正文，模板id
        result = ssender.send_with_param(86, tel, template_id, [str(code)])
        return result

    @classmethod
    def sales_msg(cls, tel, template_id, appid, appkey):
        """
        :param tel:
        :param template_id: 商品名
        :param appid: 商品名
        :param appkey: 商品名
        :return:
        """
        ssender = SmsSingleSender(appid, appkey)
        # 89021 	您的验证码是{1}，您正在使用卡来宝公众号，请勿将验证码告诉他人。
        # 108980 	您的验证码是{1}，您正在使用必备的公众号，请勿将验证码告诉他人。
        # 133521 短信正文，模板id
        result = ssender.send_with_param(86, tel, template_id, [])
        return result

    @classmethod
    def mo_short_msg(cls, content, mobile):
        """
        :param content:
        :param mobile:
        :return:
        """
        pwd = hashlib_md5(MoFangPWD).lower()
        content_gbk = content.encode('gbk')
        json = {
            "uid": MoFangUid,
            "pwd": pwd,
            "mobile": mobile,
            "content": content_gbk,
            "extnum": "638"
        }
        params = parse.urlencode(json)
        url = MoFangRequest + "?" + params
        response = requests.get(url)
        response_content = response.content
        return response_content


if __name__ == '__main__':
    pass
